﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Common.Util
{
    public class ExceptionHelper<TException>
        where TException : class
    {

        /// <summary>
        /// Возвращает полный текст Ошибки включая текст вложенных ошибок
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static string GetServiceExceptionMessage(object exception)
        {
            //var error = new System.Text.StringBuilder();
            //var exception = ex;
            //while (exception != null)
            //{
            //    error.AppendLine(exception.Message);
            //    exception = exception.InnerException;
            //}


            var ex = exception as FaultException<TException>;
            //var det = ex.Detail;

            if (ex == null)
                return string.Empty;

            string errorMessage = ex.Message;

            TException e = ex.Detail;
            var ee = e as ExceptionDetail;
            
            //TException e = ex.Detail;
            //do
            //{
            //    errorMessage += e.Message;
            //    e = e.InnerException;
            //} while (e != null);
            return errorMessage;
        }


    }

}
